import express, { Request, Response } from "express";
import AppDataSource from "../../data-source";
import { User } from "../../entity/one_to_one/user.entity";
import { Profile } from "../../entity/one_to_one/profile.entity";

const router = express.Router();

/**
 * @swagger
 * /api/onetoone/users:
 *   get:
 *     summary: 获取所有用户
 *     description: 获取系统中的所有用户列表及其关联的个人资料信息。
 *     tags:
 *       - One_To_One
 *     responses:
 *       200:
 *         description: 成功获取用户列表。
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   id:
 *                     type: integer
 *                     description: 用户ID
 *                     example: 1
 *                   name:
 *                     type: string
 *                     description: 用户姓名
 *                     example: 张三
 *                   profile:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 个人资料ID
 *                         example: 1
 *                       gender:
 *                         type: string
 *                         description: 性别
 *                         example: 男
 *                       photo:
 *                         type: string
 *                         description: 头像URL
 *                         example: http://example.com/photo.jpg
 *       500:
 *         description: 内部服务器错误。
 */
router.get("/onetoone/users", async (req, res) => {
  const users = await AppDataSource.getRepository(User).find({
    relations: { profile: true },
  });
  res.json(users);
});

/**
 * @swagger
 * /api/onetoone/users/{id}:
 *   get:
 *     summary: 根据ID获取用户
 *     description: 通过唯一标识符获取用户详细信息及其关联的个人资料信息。
 *     tags:
 *       - One_To_One
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 成功获取用户信息。
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: integer
 *                   description: 用户ID
 *                   example: 1
 *                 name:
 *                   type: string
 *                   description: 用户姓名
 *                   example: 张三
 *                 profile:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: integer
 *                       description: 个人资料ID
 *                       example: 1
 *                     gender:
 *                       type: string
 *                       description: 性别
 *                       example: 男
 *                     photo:
 *                       type: string
 *                       description: 头像URL
 *                       example: http://example.com/photo.jpg
 *       404:
 *         description: 用户未找到。
 */
router.get("/onetoone/users/:id", async (req, res): Promise<void> => {
  const results = await AppDataSource.getRepository(User).findOne({
    where: { id: Number(req.params.id) },
    relations: { profile: true },
  });
  if (!results) {
    res.status(404).send({ message: "用户未找到" });
    return;
  }
  res.send(results);
});

/**
 * @swagger
 * /api/onetoone/users:
 *   post:
 *     summary: 创建新用户
 *     description: 使用提供的详细信息创建一个新用户及其关联的个人资料。
 *     tags:
 *       - One_To_One
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 用户姓名
 *                 example: 张三
 *               profile:
 *                 type: object
 *                 properties:
 *                   gender:
 *                     type: string
 *                     description: 性别
 *                     example: 男
 *                   photo:
 *                     type: string
 *                     description: 头像URL
 *                     example: http://example.com/photo.jpg
 *     responses:
 *       201:
 *         description: 用户成功创建。
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/one_to_one_user'
 *       400:
 *         description: 请求错误 - 数据无效。
 *       500:
 *         description: 内部服务器错误。
 */
router.post("/onetoone/users", async (req, res) => {
  const userRepository = AppDataSource.getRepository(User);
  const profileRepository = AppDataSource.getRepository(Profile);

  const body = req.body;
  const user = new User();
  user.name = body.name;

  if (body.profile) {
    const profile = new Profile();
    profile.gender = body.profile.gender;
    profile.photo = body.profile.photo;
    const profileReuslt = await profileRepository.save(profile);
    console.log(profileReuslt, "profileReuslt");
    user.profile = profileReuslt;
  }

  try {
    const results = await userRepository.save(user);
    console.log(results, "user");
    res.status(201).send(results);
  } catch (error) {
    console.error("创建用户时出错:", error);
    res.status(500).send({ message: "内部服务器错误" });
  }
});

/**
 * @swagger
 * /api/onetoone/users/{id}:
 *   put:
 *     summary: 更新用户信息
 *     description: 根据ID更新现有用户的详细信息及其关联的个人资料。
 *     tags:
 *       - One_To_One
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 要更新的用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 用户姓名
 *                 example: 李四
 *               profile:
 *                 type: object
 *                 properties:
 *                   gender:
 *                     type: string
 *                     description: 性别
 *                     example: 女
 *                   photo:
 *                     type: string
 *                     description: 头像URL
 *                     example: http://example.com/newphoto.jpg
 *     responses:
 *       200:
 *         description: 用户成功更新。
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/one_to_one_user'
 *       404:
 *         description: 用户未找到。
 *       500:
 *         description: 内部服务器错误。
 */
router.put("/onetoone/users/:id", async (req, res): Promise<void> => {
  const user = await AppDataSource.getRepository(User).findOne({
    where: { id: Number(req.params.id) },
    relations: { profile: true },
  });
  if (!user) {
    res.status(404).send({ message: "用户未找到" });
    return;
  }
  const body = req.body;
  if (body.profile) {
    if (!user.profile) {
      user.profile = new Profile();
    }
    user.profile.gender = body.profile.gender;
    user.profile.photo = body.profile.photo;
    await AppDataSource.getRepository(Profile).save(user.profile);
  }
  try {
    const results = await AppDataSource.getRepository(User).save(user);
    res.send(results);
  } catch (error) {
    console.error("更新用户时出错:", error);
    res.status(500).send({ message: "内部服务器错误" });
  }
});

/**
 * @swagger
 * /api/onetoone/users/{id}:
 *   delete:
 *     summary: 删除用户
 *     description: 根据ID删除用户。
 *     tags:
 *       - One_To_One
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 要删除的用户ID
 *     responses:
 *       200:
 *         description: 用户成功删除。
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 affected:
 *                   type: integer
 *                   description: 受影响的行数
 *                   example: 1
 *       404:
 *         description: 用户未找到。
 *       500:
 *         description: 内部服务器错误。
 */
router.delete("/onetoone/users/:id", async (req, res): Promise<void> => {
  const results = await AppDataSource.getRepository(User).delete(req.params.id);
  if (results.affected === 0) {
    res.status(404).send({ message: "用户未找到" });
    return;
  }
  res.send(results);
});

export default router;
